package com.cloudxingkong.dianyinke.NewTeacher.Adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.WordWrapView;
import com.cloudxingkong.dianyinke.NewTeacher.bean.TeacherLessons;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Utils.MySwipeView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by LeeQ on 2016-4-23.
 */
public class TeacherLessonsAdapter extends BaseAdapter {
	private List<TeacherLessons.ListBean> data;
	private Context context;
	private TeacherLessonsDataCallback callback;
	private String avatarPath;
	private ListView listView;

	public TeacherLessonsAdapter (ListView listView, List<TeacherLessons.ListBean> data, Context context, TeacherLessonsDataCallback callback, String avatarPath) {
		this.listView = listView;
		this.avatarPath = avatarPath;
		this.data = data;
		this.context = context;
		this.callback = callback;

	}

	@Override
	public int getCount () {
		return data == null ? 0 : data.size ();
	}

	@Override
	public Object getItem (int position) {
		return data.get (position);
	}

	@Override
	public long getItemId (int position) {
		return position;
	}

	@Override
	public View getView (int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = View.inflate (context, R.layout.item_uploadwork, null);
			holder = new ViewHolder ();
			holder.ivCover = (CircleImageView) convertView.findViewById (R.id.uw_cover);
			holder.title = (TextView) convertView.findViewById (R.id.uw_title);
			holder.defund = (TextView) convertView.findViewById (R.id.uw_defund);
			holder.medium = (TextView) convertView.findViewById (R.id.uw_medium);
			holder.remark = (TextView) convertView.findViewById (R.id.uw_remark);
			holder.price = (TextView) convertView.findViewById (R.id.uw_price);
			holder.wwv = (WordWrapView) convertView.findViewById (R.id.uw_detail_wordwrapview);
			holder.tvDel = (TextView) convertView.findViewById (R.id.uw_view_delete);
			holder.swipeView = (MySwipeView) convertView.findViewById (R.id.uw_swipeview);
			convertView.setTag (holder);
		} else {
			holder = (ViewHolder) convertView.getTag ();
		}
		holder.swipeView.setListView (listView);
		holder.swipeView.setTag (holder.swipeView.getId (), position);


		String label = data.get (position).getLabel ();  //标签字符串
		String coverPic = data.get (position).getCoverpic ();  //封面地址
		int lessonID = data.get (position).getId ();  //课程ID
		int teacherId = data.get (position).getTeacherid ();  //教师ID
		int opID = data.get (position).getOpid ();  // opID 不知道是啥
		String price = "￥ " + data.get (position).getPrice ();//价格

		holder.tvDel.setTag ("" + position);
		callback.dataCallback (position, holder.tvDel);

		holder.title.setText (data.get (position).getTitle ());
		holder.price.setText (price);
		holder.remark.setText (data.get (position).getNote ());

		if (!avatarPath.equals ("")) {
			ImageLoader.getInstance ().displayImage (avatarPath, holder.ivCover);
		}

		//标签
		String[] split = label.split (",");
		for (int i = 0; i < split.length; i++) {
			TextView tv = new TextView (context);
			tv.setGravity (Gravity.CENTER);
			tv.setTextColor (context.getResources ().getColor (R.color.white));
			tv.setBackgroundResource (R.drawable.shape_uw_label_main);
			tv.setText (split[i]);
			if (holder.wwv.getChildCount () < split.length) {
				holder.wwv.addView (tv);
			}
//            Log.e("test", position + " -- " + holder.wwv.getChildCount() + " -- " + split.length + " -- " + split[i] + " --- " + tv.getText());
		}


		return convertView;
	}

	private class ViewHolder {
		CircleImageView ivCover;
		TextView title, defund, medium, remark, price, tvDel;
		WordWrapView wwv;
		MySwipeView swipeView;
	}

	public interface TeacherLessonsDataCallback {
		void dataCallback (int position, TextView del);
	}
}
